package algebra.query.od;

import option.QueryKeywords;
import utils.exception.CoreReasoningException;
import utils.exception.OrderingDescriptionException;
import entities.assertion.ConceptAssertion;
import entities.concept.Concept;

/**
 * A partition sort by some concept
 * 
 * @author j55wu
 * 
 */
public class OrderDepPart extends OrderDep {

	private OrderDep sub1, sub2;
	private Concept pc;

	public OrderDepPart(Concept pc, OrderDep sub1, OrderDep sub2) {
		this.pc = pc;
		this.sub1 = sub1;
		this.sub2 = sub2;
	}

	public OrderDep getSub1() {
		return sub1;
	}

	public OrderDep getSub2() {
		return sub2;
	}

	public Concept getPartitionConcept() {
		return this.pc;
	}

	protected int comparePartition(ConceptAssertion ca, ConceptAssertion ca2)
			throws OrderingDescriptionException, CoreReasoningException {
//		Concept notPartition = new NotConcept(this.pc);
		if (OdSubsumes(ca.getConcept(), this.pc, null)) {
			if (OdSubsumes(ca2.getConcept(), this.pc, null)) {
				return this.sub1.compare(ca, ca2);
			} 
			else{
				return -1;
			}
//			else if (OdSubsumes(ca2.getConcept(), notPartition, null)) {
//				return -1;
//			}
		} else 
//			if (OdSubsumes(ca.getConcept(), notPartition, null)) {
			if (OdSubsumes(ca2.getConcept(), this.pc, null)) {
				return 1;
			} 
			else {
				return this.sub2.compare(ca, ca2);
			}
//			else if (OdSubsumes(ca2.getConcept(), notPartition, null)) {
//				return this.sub2.compare(ca, ca2);
//			}
//		}
//		return 0;
	}

	@Override
	public String toString() {
		return "(OD: " + pc.toString() + " " + sub1.toString() + " "
				+ sub2.toString() + ")";
	}

	@Override
	public String toSerializedString() {
		return "(" + QueryKeywords.Od_Part + " " + this.pc.toSerializedString()
				+ " " + this.sub1.toSerializedString() + " "
				+ this.sub2.toSerializedString() + ")";
	}

	private volatile int hashCode = 0;
	@Override
	public int hashCode() {
		if (hashCode != 0) {
			return hashCode;
		}
		final int prime = 31;
		int result = 1;
		result = prime * result + ((pc == null) ? 0 : pc.hashCode());
		result = prime * result + ((sub1 == null) ? 0 : sub1.hashCode());
		result = prime * result + ((sub2 == null) ? 0 : sub2.hashCode());
		hashCode = result;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OrderDepPart other = (OrderDepPart) obj;
		if (pc == null) {
			if (other.pc != null)
				return false;
		} else if (!pc.equals(other.pc))
			return false;
		if (sub1 == null) {
			if (other.sub1 != null)
				return false;
		} else if (!sub1.equals(other.sub1))
			return false;
		if (sub2 == null) {
			if (other.sub2 != null)
				return false;
		} else if (!sub2.equals(other.sub2))
			return false;
		return true;
	}
	
	

}
